<template>
<div class="management-merchant views-container">
    <el-dialog @close="resetDialogFormData({is_popular: 1,title: '', content: '', image: [], sort:''})" :visible.sync="dialogTableVisible" custom-class="reset-pwd wlm-form-dialog dialog-table">
        <div class="wlm-form" v-if="dialogTableVisible">
            <div class="wlm-form-header wlm-dialog-fixheader">
                <span class="wlm-dialog-inlenr">{{`${!!formFormatData.api['editForm']['params']['id']?'编辑':'添加'}分类`}}</span>
            </div>
            <el-scrollbar wrap-class="scrollbar-wrapper">
                <div class="wlm-form-content">
                    <div class="wlm-dialog-main">
                        <div class="wlm-dialog-content">
                            <el-form ref="userForm" :model="formFormatData.formData" :rules="formFormatData.rules" size="small" label-width="140px" class="retail-form" label-position="right">
                                <el-form-item label="热门">
                                    <el-radio-group v-model="formFormatData.formData.is_popular">
                                        <el-radio :label="1">是</el-radio>
                                        <el-radio :label="2">否</el-radio>
                                    </el-radio-group>
                                </el-form-item>
                                <el-form-item label="名称" prop="title">
                                    <el-input v-model.trim="formFormatData.formData.title">
                                    </el-input>
                                </el-form-item>
                                <el-form-item label="背景图" prop="merchantlog" ref="merchantlog">
                                    <choose-files ref="chooseFiles" :config="chooseLogoConfig" :changeEvt="formFormatFiltersChange"></choose-files>
                                    <div class="form-help">第一张为全局展示图，第二张为话题列表页背景图</div>
                                </el-form-item>
                                <!-- <el-form-item label="话题列表页背景图" prop="businessbackgroundmap" ref="businessbackgroundmap">
                                    <choose-files ref="chooseFiles" :config="chooseBgConfig" :changeEvt="formFormatFiltersChange"></choose-files>
                                </el-form-item> -->
                                <el-form-item label="简介" prop="content">
                                    <el-input type="textarea" v-model="formFormatData.formData.content" maxlength="230">
                                    </el-input>
                                </el-form-item>
                                <el-form-item label="排序" prop="sort">
                                    <el-input v-model="formFormatData.formData.sort">
                                    </el-input>
                                    <div class="form-help">数字越大越靠前</div>
                                </el-form-item>
                            </el-form>
                        </div>
                    </div>
                </div>
            </el-scrollbar>
        </div>
        <footer class="flex-row flex-align-c flex-justify-c wlm-dialog-fixfooter">
            <el-button size="small" @click="formFormatSubmit" type="primary">保存</el-button>
        </footer>
    </el-dialog>

    <div class="wlm-table">
        <div class="wlm-table-hearder-btn">
            <el-button size="small" type="primary" @click="dialogTableVisible = true">添加</el-button>
        </div>
        <div class="wlm-table-header">
            <el-form :model="tableFormatData.userTable.files" size="small" label-width="80px" class="retail-form" label-position="right">
                <el-form-item label="话题名称：" class="search">
                    <el-input v-model="tableFormatData.userTable.files.content" placeholder="请输入内容" style="width:160px;"></el-input>
                </el-form-item>
                <el-form-item label="" class="filter-btns">
                    <el-button v-waves type="primary" @click="filesSerch">筛选</el-button>
                    <el-button type="text" @click="filesEmpty">清空筛选条件</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="wlm-table-content">
            <el-table :ref="tableFormatData.userTable.key" :default-sort="{prop: 'sort', order: 'descending'}" :data="tableFormatData.userTable.tableData" @selection-change="handleSelectionChange" style="width: 100%">
                <el-table-column prop="sort" width="80" label="排序" align="center" sortable>
                    <template slot-scope="scope">
                        <el-input-number @blur="changeTableItem({id:scope.row.id,sort:scope.row.sort},tableFormatData.userTable.change.sort)" style="width:100%;" size="mini" v-model="scope.row.sort" :controls="false"></el-input-number>
                    </template>
                </el-table-column>
                <el-table-column prop="date" label="名称" align="center">
                    <template slot-scope="scope">
                        <span>{{scope.row.title}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="date" label="帖子数量" align="center">
                    <template slot-scope="scope">
                        <span>{{scope.row.shop_num}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center" min-width="150">
                    <template slot-scope="scope">
                        <div class="operation-group">
                            <el-button class="wlm-text" type="text" @click="formFormatDialogEditEvt({id:scope.row.id,toggle:'dialogTableVisible'})">编辑</el-button>
                            <div class="btn-line"></div>
                            <el-button class="wlm-text" type="text" @click="delTableItem(scope.row.id)">删除</el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-content flex-row flex-justify-e flex-align-c">
                <el-pagination :disabled="!hasTableData" @size-change="listPageChange" @current-change="listPageChange" :current-page.sync="tableFormatData.userTable.pagination.page" :page-sizes="tableFormatData.userTable.pagination.pagesizes" :page-size.sync="tableFormatData.userTable.pagination.list_rows" layout="total, sizes, prev, pager, next, jumper" :total="hasTableData?tableFormatData.userTable.pagination.total : 0">
                </el-pagination>
            </div>
        </div>
    </div>
</div>
</template>

<script>
// import upload from '@/components/Upload/index'
import ChooseFiles from '@/components/ChooseFiles/index'
import {
  topicAddOrEdit,
  editGetTopicData,
  delTopicData,
  getTopicList,
  editTopicSort
} from '@/api/merchant'
import waves from '@/directive/waves' // Waves directive
import mixins from '@/mixins/mixins'
export default {
  mixins: [mixins.getters('Table'), mixins.getters('Form')],
  name: 'UserPlatform',
  components: {
    // upload,
    ChooseFiles
  },
  directives: {
    waves
  },
  data() {
    var checkImg = (rule, value, callback) => {
      const val = this.formFormatData.formData[rule.bindKey]
      const { message } = rule
      if (!Array.isArray(val) || (Array.isArray(val) && val.length !== 2)) {
        console.log('xx')
        return callback(new Error(`${message}！~`))
      } else {
        callback()
      }
    }
    return {
      dialogTableVisible: false,
      chooseLogoConfig: {
        type: ['image'],
        model: 'display',
        uploadConfig: {
          limit: 2,
          model: 'display'
        },
        initList: [],
        isShowList: true,
        file_type: 'image',
        bindKey: {
          key: 'image',
          model: 'img',
          validateField: 'merchantlog'
        }
      },
      formFormatData: {
        key: 'userForm',
        model: {
          noCreateEdit: false,
          bindDialog: 'dialogTableVisible'
        },
        api: {
          editForm: {
            api: editGetTopicData,
            params: {
              id: ''
            },
            filters: {
              img: {
                bindInit: ['chooseLogoConfig']
              }
            }
          },
          submitForm: topicAddOrEdit
        },
        formData: {
          is_popular: 1,
          title: '',
          content: '',
          sort: '',
          image: []
        },
        rules: {
          title: [{
            required: true,
            message: '请输入名称',
            trigger: 'blur'
          }],
          content: [{
            required: true,
            message: '请输入简介',
            trigger: 'blur'
          }],
          sort: [{
            required: true,
            message: '请输入排序',
            trigger: 'blur'
          }],
          merchantlog: [{
            required: true,
            bindKey: 'image',
            message: '请确认图片上传!',
            validator: checkImg,
            trigger: 'change'
          }]
        }
      },
      tableFormatData: {
        current: 'userTable',
        userTable: {
          key: 'userTable',
          api: {
            getList: getTopicList,
            delList: delTopicData
          },
          tableData: [],
          files: {
            content: '',
            ids: [],
            checkall: '0',
            Recycle: '1'
          },
          change: {
            sort: {
              name: '排序',
              key: 'sort',
              api: editTopicSort
            }
          },
          pagination: {
            page: 1,
            list_rows: 10,
            pagesizes: [10, 20, 50],
            total: 0
          }
        }
      }
    }
  },
  methods: {
    formFormatCallBack(data, type, response) {
      if (type === 'submitForm') {
        const { data = {}} = response
        if (data.code === 1) {
          this.filesSerch()
          this.dialogTableVisible = false
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.management-merchant {
    .logo {
        height: 32px;
        width: 32px;
        border-radius: 32px;
        vertical-align: middle;
    }
}
</style>
